// pages/group/index.js
var rtcroom = require('../../utils/rtcroom.js');
var getlogininfo = require('../../getlogininfo.js');
var app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    listData: [],
    page: 1,
    loading: 0,
    lastpage: 0,
    groupData: {}
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    // console.log(options);
    var window = app.globalData.window;
    window.windowHeight = window.windowHeight - 30;
    this.setData({ window: window, id: 99 })
   // this.setData({ window: window, id: 91 })
    app.getUserInfo((userInfo)=>{
      this.data.userInfo = userInfo;
      console.log(userInfo);
      this.getGroup();
      this.getTopicList();
    })
    
  },

  //
  getGroup: function () {
    var that = this;
    var id = this.data.id;
    app.api("sns/group/find", {
      id: id,
    }, function (res) {
      if (res.code != 200) {
        app.alert(res.msg);
        return;
      }
      wx.setNavigationBarTitle({
        title: res.data.value
      });
      if (res.data.content) {
        res.data.content = JSON.parse(res.data.content);
      }
      that.setData({ groupData: res.data });
    })
  },


  //api evens/list 活动列表 
  getTopicList: function (reset = 0) {
    var that = this;
    // var order_by = that.data.showOrder;
    //重新加载 
    if (reset == 1) {
      that.setData({ page: 1, listData: [], loading: 0, lastpage: 0 });
      app.setPageData("sns/group/index", { reload: 0 });
    }
    if (this.data.loading == 1 || this.data.lastpage == 1) {
      return false;
    }
    this.data.loading = 1;
    //   lastpage:0,
    var page = that.data.page;
    //console.log(event_type);
    app.api("sns/topic/list", {
      page: page,
      //status: 1,
      type: 'faceEngTopic',
      //type: 'groupTopic',
      order: 'id desc',
      limit: 15,
    }, function (res) {
      that.data.loading = 0;
      if (res.code != 200) {
        app.alert(res.msg);
        return;
      }
      if (res.data.list.length == 0) {
        that.data.lastpage = 1;
      }
      page++;
      that.setData({ page: page, listData: that.data.listData.concat(res.data.list) });

    })
  },
  thumb_up: function (e) {//列表点赞 
    var that = this;
    var index = e.currentTarget.dataset.id;
    var topic_id = e.currentTarget.dataset.topic_id;
    app.thumb_up(topic_id, function (res) {
      var topicData = that.data.listData[index];
      if (res.type == "add") {
        topicData.good_count++;
        topicData.thumb_up_ed = 1;
      } else if (res.type == "del") {
        topicData.good_count--;
        topicData.thumb_up_ed = 2;
      }
      that.data.listData[index] = topicData;
      that.setData({
        listData: that.data.listData
      });
    })
  },

	/**
	 * 生命周期函数--监听页面初次渲染完成
	 */
  onReady: function () {
    // 先获取登录信息
    var self = this;
    wx.showLoading({
      title: '登录信息获取中',
    })
    getlogininfo.getLoginInfo({
      type: 'double_room',
      success: function (ret) {
        self.data.firstshow = false;
        self.data.isGetLoginInfo = true;
        // self.getRoomList();
        console.log('我的昵称：', ret.userName);
        self.setData({
          userName: ret.userName
        });
        wx.hideLoading();
      },
      fail: function (ret) {
        self.data.isGetLoginInfo = false;
        wx.hideLoading();
        wx.showModal({
          title: '获取登录信息失败',
          content: ret.errMsg,
          showCancel: false,
          complete: function () {
            wx.navigateBack({});
          }
        });
      }
    });
  },

	/**
	 * [create 创建房间，进入创建页面]
	 * @return {[type]} [description]
	 */
  create: function (e) {
    var self = this;
    // 防止两次点击操作间隔太快
    var nowTime = new Date();
    if (nowTime - this.data.tapTime < 1000) {
      return;
    }
    if (!self.data.isGetLoginInfo) {
      wx.showModal({
        title: '提示',
        content: '登录信息初始化中，请稍后再试',
        showCancel: false
      })
      return;
    }
    var user_id = e.currentTarget.dataset.user_id;
    var roomName = e.currentTarget.dataset.title;
    var url = '/pages/doubleroom/room/room?roomID=bestsmart_room_' + user_id + '&userName=' + self.data.userName + "&roomName=" + roomName;
    //var url = '/pages/doubleroom/room/room?type=create&roomName=test&userName=' + self.data.userName;
    wx.navigateTo({
      url: url
    });
    self.setData({ 'tapTime': nowTime });
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    var reload = app.getPageData("group/index", 'reload');
    if (reload == 1) {
      this.getTopicList(1);
      this.getGroup();
    }
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },


  //下拉刷新
  onPullDownRefresh: function () {
    this.getTopicList(1);
    wx.stopPullDownRefresh();
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    this.getTopicList();
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  },
  scrolltolower: function (e) {
    console.log(e);
  },
  apply1: function () {
    // wx.navigateTo({
    //   url: '/pages/index/issue?pid=' + this.data.id
    // })
    wx.navigateTo({
      url: '/pages/group/list',
    })
     
  },
  thumb_list_up: function (e) {//列表点赞 
   var that = this;
   var index = e.currentTarget.dataset.id;
   var topic_id = e.currentTarget.dataset.topic_id;
   app.thumb_up(topic_id, function (res) {
     var topicData = that.data.listData[index];
     if (res.type == "add") {
       topicData.good_count++;
       topicData.thumb_up_ed = 1;
     } else if (res.type == "del") {
       topicData.good_count--;
       topicData.thumb_up_ed = 2;
     }
     that.data.listData[index] = topicData;
     that.setData({
       listData: that.data.listData
     });
   })
   // console.log(topicData);
 },
  //浏览图片
  previewListImage: function (e) {
    var current = e.target.dataset.src;
    var index = e.target.dataset.index;
    var pics = this.data.listData[index].pics;
    wx.previewImage({
      current: current,
      urls: pics
    })
  },
})